import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Layout from '@/layout/index.vue';
import store from '@/store'
import Menu from '@/router/Menu'
const history = createWebHistory()
const routes: Array<RouteRecordRaw> = [
    {
        path: "/login",
        name: "login",
        component: () => import("@/layout/login.vue"),
    },

    {
        path: '/',
        name: 'dashboard',
        redirect: '/dashboard',
        component: Layout,
        children: [
            {
                path: 'dashboard',
                name: 'dashboard',
                component: () => import('@/layout/dashboard.vue'),
                meta: {
                    title: '工作台',
                    menuId: "",
                    icon: 'ali_home_4',
                    auth: [],
                }
            }
        ]
    },
];
const router = createRouter({
    history,
    routes
})
router.beforeEach(async (to, from, next) => {
    NProgress.start();
    const isLogin: boolean = store.getters.isLogin;
    const hasGenerateMenu: boolean = store.state.hasGenerateMenu;
    if (isLogin) {
        if (hasGenerateMenu) {
            if (to.path === '/login') {
                next({
                    path: '/'
                })
            } else {
                store.commit('addKeepAlive', to.name);
                next()
            }
        } else {
            store.commit("setMenu", Menu);
            next({
                ...to,
                replace: true
            })
        }
    } else {
        if (to.path === '/login') {
            next()
        } else {
            next({
                path: '/login'
            })
        }
    }
    // next();
})
router.afterEach(() => {
    NProgress.done();
});
export default router